import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import Chat from 'src/entities/chat.entity';
import Friend from 'src/entities/friend.entity';
import User from 'src/entities/user.entity';
import { Repository } from 'typeorm';

@Injectable()
export class ChatService {
  constructor(
    @InjectRepository(User) private userRepository: Repository<User>,
    @InjectRepository(Friend) private friendRepository: Repository<Friend>,
  ) {}

  async find(userId: string) {
    const chatList = await this.friendRepository.find({ where: { userId: userId } });

    const arr = [];
    for (const item of chatList) {
      const user = await this.userRepository.findOne({ where: { id: item.friendId } });
      arr.push({
        friendId: item.friendId,
        avatar: user.avatar,
        nickName: user.nickname,
      });
    }

    return arr;
  }
}
